package cn.chinasyq.photoquan.user.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.user.bean.Album;
import cn.chinasyq.photoquan.util.TimeUtil;
import cn.master.volley.commons.VolleyHelper;

/**
 * Created by 一搏 on 2015/12/17.
 */
public class AlbumListAdapter extends BaseAdapter {
    private ArrayList<Album> albums;

    public AlbumListAdapter() {
        albums = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return albums.size();
    }

    @Override
    public Object getItem(int position) {
        return albums.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    String MM = "MM";
    String dd = "dd";

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        Album item = (Album) getItem(position);
        if (convertView == null || convertView.getTag() == null || !(convertView.getTag() instanceof ViewHolder)) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_alums, null);
            viewHolder.iv_1 = (NetworkImageView) convertView.findViewById(R.id.iv_1);
            viewHolder.iv_2 = (NetworkImageView) convertView.findViewById(R.id.iv_2);
            viewHolder.iv_3 = (NetworkImageView) convertView.findViewById(R.id.iv_3);
            viewHolder.tv_d = (TextView) convertView.findViewById(R.id.tv_d);
            viewHolder.tv_m = (TextView) convertView.findViewById(R.id.tv_m);
            viewHolder.tv_dian = (TextView) convertView.findViewById(R.id.tv_dian);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_d.setText(new SimpleDateFormat(dd).format(new Date(item.getCreated_at()*1000)));
        viewHolder.tv_m.setText(TimeUtil.mmToMM(new SimpleDateFormat(MM).format(new Date(item.getCreated_at()*1000))));
        viewHolder.tv_dian.setVisibility(item.getPhotos().size() > 3 ? View.VISIBLE : View.INVISIBLE);

        try {
            viewHolder.iv_1.setImageUrl(item.getPhotos().get(0).getImage(), VolleyHelper.getImageLoader());
            viewHolder.iv_1.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            viewHolder.iv_1.setVisibility(View.INVISIBLE);
        }
        try {
            viewHolder.iv_2.setImageUrl(item.getPhotos().get(1).getImage(), VolleyHelper.getImageLoader());
            viewHolder.iv_2.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            viewHolder.iv_2.setVisibility(View.INVISIBLE);
        }
        try {
            viewHolder.iv_3.setImageUrl(item.getPhotos().get(2).getImage(), VolleyHelper.getImageLoader());
            viewHolder.iv_3.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            viewHolder.iv_3.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    class ViewHolder {
        TextView tv_d, tv_m, tv_dian;
        NetworkImageView iv_1, iv_2, iv_3;
    }

    public void setAlbums(ArrayList<Album> albums) {
        this.albums = albums;
        notifyDataSetChanged();
    }

    public void addAlbums(List<Album> albums) {
        this.albums.addAll(albums);
        notifyDataSetChanged();
    }
}
